package com.ruoyi.boat.service.impl;

import com.ruoyi.boat.domain.Device;
import com.ruoyi.boat.mapper.DeviceMapper;
import com.ruoyi.boat.service.IDeviceBoatService;
import com.ruoyi.boat.service.IDeviceService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Service
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private IDeviceBoatService deviceBoatService;

    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Device selectDeviceById(Long id) {
        return deviceMapper.selectDeviceById(id);
    }

    /**
     * 查询设备列表
     *
     * @param device 设备
     * @return 设备
     */
    @Override
    public List<Device> selectDeviceList(Device device) {
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 查询设备列表
     *
     * @param boatId
     * @return
     */
    @Override
    public List<Device> selectDeviceListByBoatId(Long boatId) {
        List<Long> deviceBoats = deviceBoatService.selectDeviceIdListByBoatId(boatId);
        return deviceMapper.selectDeviceList(new Device())
                .stream().filter(device -> deviceBoats.contains(device.getId()))
                .sorted(Comparator.comparing(Device::getOrderNum))
                .collect(Collectors.toList());
    }

    /**
     * 新增设备
     *
     * @param device 设备
     * @return 结果
     */
    @Override
    public int insertDevice(Device device) {
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改设备
     *
     * @param device 设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device) {
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(String ids) {
        return deviceMapper.deleteDeviceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceById(Long id) {
        return deviceMapper.deleteDeviceById(id);
    }
}
